package net.mingsoft.report_data.util;

import net.mingsoft.report_data.constant.ZkDataConstant;

import java.math.BigDecimal;

/**
 * @author Administrator
 */
public class MathUtil {
    private MathUtil() {}

    /**
     * 计算：增长率百分数 = (b-a) / a * 100
     * @param a a
     * @param b b
     * @return 增长率百分数
     */
    public static String getGrowth(Long a, Long b) {
        final BigDecimal aD = BigDecimal.valueOf(a);
        final BigDecimal bD = BigDecimal.valueOf(b);
        return bD.subtract(aD).divide(bD, 4, BigDecimal.ROUND_DOWN)
                .multiply(ZkDataConstant.HUNDRED_DECIMAL).setScale(2, BigDecimal.ROUND_DOWN).toPlainString();
    }

    /**
     * 计算：占比
     * @param a a
     * @param b b
     * @return 占比
     */
    public static String getRate(Long a, Long b) {
        if (b == 0) {
            return "0";
        }
        final BigDecimal aD = BigDecimal.valueOf(a);
        final BigDecimal bD = BigDecimal.valueOf(b);
        return aD.divide(bD, 4, BigDecimal.ROUND_DOWN)
                .multiply(ZkDataConstant.HUNDRED_DECIMAL).setScale(2, BigDecimal.ROUND_DOWN).toPlainString();
    }

    public static String getRate(int a, int b) {
        if (b == 0) {
            return "0";
        }
        final BigDecimal aD = BigDecimal.valueOf(a);
        final BigDecimal bD = BigDecimal.valueOf(b);
        return aD.divide(bD, 4, BigDecimal.ROUND_DOWN)
                .multiply(ZkDataConstant.HUNDRED_DECIMAL).setScale(2, BigDecimal.ROUND_DOWN).toPlainString();
    }

    public static String getTenThousand(long value) {
        if (value == 0) {
            return "0";
        }
        return BigDecimal.valueOf(value).divide(BigDecimal.valueOf(10000), 2, 2).toPlainString();
    }
}
